<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>19.7. mimetypes — Map filenames to MIME types &mdash; Python v2.6.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.2 documentation" href="../index.html" />
    <link rel="up" title="19. Internet Data Handling" href="netdata.html" />
    <link rel="next" title="19.8. MimeWriter — Generic MIME file writer" href="mimewriter.html" />
    <link rel="prev" title="19.6. mimetools — Tools for parsing MIME messages" href="mimetools.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="mimewriter.html" title="19.8. MimeWriter — Generic MIME file writer"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="mimetools.html" title="19.6. mimetools — Tools for parsing MIME messages"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="netdata.html" accesskey="U">19. Internet Data Handling</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-mimetypes">
<h1>19.7. <tt class="xref docutils literal"><span class="pre">mimetypes</span></tt> &#8212; Map filenames to MIME types<a class="headerlink" href="#module-mimetypes" title="Permalink to this headline">¶</a></h1>
<p id="index-415">The <tt class="xref docutils literal"><span class="pre">mimetypes</span></tt> module converts between a filename or URL and the MIME type
associated with the filename extension.  Conversions are provided from filename
to MIME type and from MIME type to filename extension; encodings are not
supported for the latter conversion.</p>
<p>The module provides one class and a number of convenience functions. The
functions are the normal interface to this module, but some applications may be
interested in the class as well.</p>
<p>The functions described below provide the primary interface for this module.  If
the module has not been initialized, they will call <a title="mimetypes.init" class="reference internal" href="#mimetypes.init"><tt class="xref docutils literal"><span class="pre">init()</span></tt></a> if they rely on
the information <a title="mimetypes.init" class="reference internal" href="#mimetypes.init"><tt class="xref docutils literal"><span class="pre">init()</span></tt></a> sets up.</p>
<dl class="function">
<dt id="mimetypes.guess_type">
<tt class="descclassname">mimetypes.</tt><tt class="descname">guess_type</tt><big>(</big><em>filename</em><span class="optional">[</span>, <em>strict</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mimetypes.guess_type" title="Permalink to this definition">¶</a></dt>
<dd><p id="index-416">Guess the type of a file based on its filename or URL, given by <em>filename</em>.  The
return value is a tuple <tt class="docutils literal"><span class="pre">(type,</span> <span class="pre">encoding)</span></tt> where <em>type</em> is <tt class="xref docutils literal"><span class="pre">None</span></tt> if the
type can&#8217;t be guessed (missing or unknown suffix) or a string of the form
<tt class="docutils literal"><span class="pre">'type/subtype'</span></tt>, usable for a MIME <em>content-type</em> header.</p>
<p><em>encoding</em> is <tt class="xref docutils literal"><span class="pre">None</span></tt> for no encoding or the name of the program used to encode
(e.g. <strong>compress</strong> or <strong>gzip</strong>). The encoding is suitable for use
as a <em>Content-Encoding</em> header, <em>not</em> as a
<em>Content-Transfer-Encoding</em> header. The mappings are table driven.
Encoding suffixes are case sensitive; type suffixes are first tried case
sensitively, then case insensitively.</p>
<p>Optional <em>strict</em> is a flag specifying whether the list of known MIME types
is limited to only the official types <a class="reference external" href="http://www.iana.org/assignments/media-types/">registered with IANA</a> are recognized.
When <em>strict</em> is true (the default), only the IANA types are supported; when
<em>strict</em> is false, some additional non-standard but commonly used MIME types
are also recognized.</p>
</dd></dl>

<dl class="function">
<dt id="mimetypes.guess_all_extensions">
<tt class="descclassname">mimetypes.</tt><tt class="descname">guess_all_extensions</tt><big>(</big><em>type</em><span class="optional">[</span>, <em>strict</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mimetypes.guess_all_extensions" title="Permalink to this definition">¶</a></dt>
<dd><p>Guess the extensions for a file based on its MIME type, given by <em>type</em>. The
return value is a list of strings giving all possible filename extensions,
including the leading dot (<tt class="docutils literal"><span class="pre">'.'</span></tt>).  The extensions are not guaranteed to have
been associated with any particular data stream, but would be mapped to the MIME
type <em>type</em> by <a title="mimetypes.guess_type" class="reference internal" href="#mimetypes.guess_type"><tt class="xref docutils literal"><span class="pre">guess_type()</span></tt></a>.</p>
<p>Optional <em>strict</em> has the same meaning as with the <a title="mimetypes.guess_type" class="reference internal" href="#mimetypes.guess_type"><tt class="xref docutils literal"><span class="pre">guess_type()</span></tt></a> function.</p>
</dd></dl>

<dl class="function">
<dt id="mimetypes.guess_extension">
<tt class="descclassname">mimetypes.</tt><tt class="descname">guess_extension</tt><big>(</big><em>type</em><span class="optional">[</span>, <em>strict</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mimetypes.guess_extension" title="Permalink to this definition">¶</a></dt>
<dd><p>Guess the extension for a file based on its MIME type, given by <em>type</em>. The
return value is a string giving a filename extension, including the leading dot
(<tt class="docutils literal"><span class="pre">'.'</span></tt>).  The extension is not guaranteed to have been associated with any
particular data stream, but would be mapped to the  MIME type <em>type</em> by
<a title="mimetypes.guess_type" class="reference internal" href="#mimetypes.guess_type"><tt class="xref docutils literal"><span class="pre">guess_type()</span></tt></a>.  If no extension can be guessed for <em>type</em>, <tt class="xref docutils literal"><span class="pre">None</span></tt> is
returned.</p>
<p>Optional <em>strict</em> has the same meaning as with the <a title="mimetypes.guess_type" class="reference internal" href="#mimetypes.guess_type"><tt class="xref docutils literal"><span class="pre">guess_type()</span></tt></a> function.</p>
</dd></dl>

<p>Some additional functions and data items are available for controlling the
behavior of the module.</p>
<dl class="function">
<dt id="mimetypes.init">
<tt class="descclassname">mimetypes.</tt><tt class="descname">init</tt><big>(</big><span class="optional">[</span><em>files</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mimetypes.init" title="Permalink to this definition">¶</a></dt>
<dd>Initialize the internal data structures.  If given, <em>files</em> must be a sequence
of file names which should be used to augment the default type map.  If omitted,
the file names to use are taken from <a title="mimetypes.knownfiles" class="reference internal" href="#mimetypes.knownfiles"><tt class="xref docutils literal"><span class="pre">knownfiles</span></tt></a>.  Each file named in
<em>files</em> or <a title="mimetypes.knownfiles" class="reference internal" href="#mimetypes.knownfiles"><tt class="xref docutils literal"><span class="pre">knownfiles</span></tt></a> takes precedence over those named before it.
Calling <a title="mimetypes.init" class="reference internal" href="#mimetypes.init"><tt class="xref docutils literal"><span class="pre">init()</span></tt></a> repeatedly is allowed.</dd></dl>

<dl class="function">
<dt id="mimetypes.read_mime_types">
<tt class="descclassname">mimetypes.</tt><tt class="descname">read_mime_types</tt><big>(</big><em>filename</em><big>)</big><a class="headerlink" href="#mimetypes.read_mime_types" title="Permalink to this definition">¶</a></dt>
<dd>Load the type map given in the file <em>filename</em>, if it exists.  The  type map is
returned as a dictionary mapping filename extensions, including the leading dot
(<tt class="docutils literal"><span class="pre">'.'</span></tt>), to strings of the form <tt class="docutils literal"><span class="pre">'type/subtype'</span></tt>.  If the file <em>filename</em>
does not exist or cannot be read, <tt class="xref docutils literal"><span class="pre">None</span></tt> is returned.</dd></dl>

<dl class="function">
<dt id="mimetypes.add_type">
<tt class="descclassname">mimetypes.</tt><tt class="descname">add_type</tt><big>(</big><em>type</em>, <em>ext</em><span class="optional">[</span>, <em>strict</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mimetypes.add_type" title="Permalink to this definition">¶</a></dt>
<dd><p>Add a mapping from the mimetype <em>type</em> to the extension <em>ext</em>. When the
extension is already known, the new type will replace the old one. When the type
is already known the extension will be added to the list of known extensions.</p>
<p>When <em>strict</em> is True (the default), the mapping will added to the official MIME
types, otherwise to the non-standard ones.</p>
</dd></dl>

<dl class="data">
<dt id="mimetypes.inited">
<tt class="descclassname">mimetypes.</tt><tt class="descname">inited</tt><a class="headerlink" href="#mimetypes.inited" title="Permalink to this definition">¶</a></dt>
<dd>Flag indicating whether or not the global data structures have been initialized.
This is set to true by <a title="mimetypes.init" class="reference internal" href="#mimetypes.init"><tt class="xref docutils literal"><span class="pre">init()</span></tt></a>.</dd></dl>

<dl class="data">
<dt id="mimetypes.knownfiles">
<tt class="descclassname">mimetypes.</tt><tt class="descname">knownfiles</tt><a class="headerlink" href="#mimetypes.knownfiles" title="Permalink to this definition">¶</a></dt>
<dd><p id="index-417">List of type map file names commonly installed.  These files are typically named
<tt class="docutils literal"><span class="pre">mime.types</span></tt> and are installed in different locations by different
packages.</p>
</dd></dl>

<dl class="data">
<dt id="mimetypes.suffix_map">
<tt class="descclassname">mimetypes.</tt><tt class="descname">suffix_map</tt><a class="headerlink" href="#mimetypes.suffix_map" title="Permalink to this definition">¶</a></dt>
<dd>Dictionary mapping suffixes to suffixes.  This is used to allow recognition of
encoded files for which the encoding and the type are indicated by the same
extension.  For example, the <tt class="docutils literal"><span class="pre">.tgz</span></tt> extension is mapped to <tt class="docutils literal"><span class="pre">.tar.gz</span></tt>
to allow the encoding and type to be recognized separately.</dd></dl>

<dl class="data">
<dt id="mimetypes.encodings_map">
<tt class="descclassname">mimetypes.</tt><tt class="descname">encodings_map</tt><a class="headerlink" href="#mimetypes.encodings_map" title="Permalink to this definition">¶</a></dt>
<dd>Dictionary mapping filename extensions to encoding types.</dd></dl>

<dl class="data">
<dt id="mimetypes.types_map">
<tt class="descclassname">mimetypes.</tt><tt class="descname">types_map</tt><a class="headerlink" href="#mimetypes.types_map" title="Permalink to this definition">¶</a></dt>
<dd>Dictionary mapping filename extensions to MIME types.</dd></dl>

<dl class="data">
<dt id="mimetypes.common_types">
<tt class="descclassname">mimetypes.</tt><tt class="descname">common_types</tt><a class="headerlink" href="#mimetypes.common_types" title="Permalink to this definition">¶</a></dt>
<dd>Dictionary mapping filename extensions to non-standard, but commonly found MIME
types.</dd></dl>

<p>The <a title="mimetypes.MimeTypes" class="reference internal" href="#mimetypes.MimeTypes"><tt class="xref docutils literal"><span class="pre">MimeTypes</span></tt></a> class may be useful for applications which may want more
than one MIME-type database:</p>
<dl class="class">
<dt id="mimetypes.MimeTypes">
<em class="property">
class </em><tt class="descclassname">mimetypes.</tt><tt class="descname">MimeTypes</tt><big>(</big><span class="optional">[</span><em>filenames</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mimetypes.MimeTypes" title="Permalink to this definition">¶</a></dt>
<dd><p>This class represents a MIME-types database.  By default, it provides access to
the same database as the rest of this module. The initial database is a copy of
that provided by the module, and may be extended by loading additional
<tt class="docutils literal"><span class="pre">mime.types</span></tt>-style files into the database using the <a title="mimetypes.MimeTypes.read" class="reference internal" href="#mimetypes.MimeTypes.read"><tt class="xref docutils literal"><span class="pre">read()</span></tt></a> or
<a title="mimetypes.MimeTypes.readfp" class="reference internal" href="#mimetypes.MimeTypes.readfp"><tt class="xref docutils literal"><span class="pre">readfp()</span></tt></a> methods.  The mapping dictionaries may also be cleared before
loading additional data if the default data is not desired.</p>
<p>The optional <em>filenames</em> parameter can be used to cause additional files to be
loaded &#8220;on top&#8221; of the default database.</p>
<p>
<span class="versionmodified">New in version 2.2.</span></p>
</dd></dl>

<p>An example usage of the module:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mimetypes</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mimetypes</span><span class="o">.</span><span class="n">init</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mimetypes</span><span class="o">.</span><span class="n">knownfiles</span>
<span class="go">[&#39;/etc/mime.types&#39;, &#39;/etc/httpd/mime.types&#39;, ... ]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mimetypes</span><span class="o">.</span><span class="n">suffix_map</span><span class="p">[</span><span class="s">&#39;.tgz&#39;</span><span class="p">]</span>
<span class="go">&#39;.tar.gz&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mimetypes</span><span class="o">.</span><span class="n">encodings_map</span><span class="p">[</span><span class="s">&#39;.gz&#39;</span><span class="p">]</span>
<span class="go">&#39;gzip&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mimetypes</span><span class="o">.</span><span class="n">types_map</span><span class="p">[</span><span class="s">&#39;.tgz&#39;</span><span class="p">]</span>
<span class="go">&#39;application/x-tar-gz&#39;</span>
</pre></div>
</div>
<div class="section" id="mimetypes-objects">
<span id="id1"></span><h2>19.7.1. MimeTypes Objects<a class="headerlink" href="#mimetypes-objects" title="Permalink to this headline">¶</a></h2>
<p><a title="mimetypes.MimeTypes" class="reference internal" href="#mimetypes.MimeTypes"><tt class="xref docutils literal"><span class="pre">MimeTypes</span></tt></a> instances provide an interface which is very like that of the
<tt class="xref docutils literal"><span class="pre">mimetypes</span></tt> module.</p>
<dl class="attribute">
<dt id="mimetypes.MimeTypes.suffix_map">
<tt class="descclassname">MimeTypes.</tt><tt class="descname">suffix_map</tt><a class="headerlink" href="#mimetypes.MimeTypes.suffix_map" title="Permalink to this definition">¶</a></dt>
<dd>Dictionary mapping suffixes to suffixes.  This is used to allow recognition of
encoded files for which the encoding and the type are indicated by the same
extension.  For example, the <tt class="docutils literal"><span class="pre">.tgz</span></tt> extension is mapped to <tt class="docutils literal"><span class="pre">.tar.gz</span></tt>
to allow the encoding and type to be recognized separately.  This is initially a
copy of the global <tt class="docutils literal"><span class="pre">suffix_map</span></tt> defined in the module.</dd></dl>

<dl class="attribute">
<dt id="mimetypes.MimeTypes.encodings_map">
<tt class="descclassname">MimeTypes.</tt><tt class="descname">encodings_map</tt><a class="headerlink" href="#mimetypes.MimeTypes.encodings_map" title="Permalink to this definition">¶</a></dt>
<dd>Dictionary mapping filename extensions to encoding types.  This is initially a
copy of the global <tt class="docutils literal"><span class="pre">encodings_map</span></tt> defined in the module.</dd></dl>

<dl class="attribute">
<dt id="mimetypes.MimeTypes.types_map">
<tt class="descclassname">MimeTypes.</tt><tt class="descname">types_map</tt><a class="headerlink" href="#mimetypes.MimeTypes.types_map" title="Permalink to this definition">¶</a></dt>
<dd>Dictionary mapping filename extensions to MIME types.  This is initially a copy
of the global <tt class="docutils literal"><span class="pre">types_map</span></tt> defined in the module.</dd></dl>

<dl class="attribute">
<dt id="mimetypes.MimeTypes.common_types">
<tt class="descclassname">MimeTypes.</tt><tt class="descname">common_types</tt><a class="headerlink" href="#mimetypes.MimeTypes.common_types" title="Permalink to this definition">¶</a></dt>
<dd>Dictionary mapping filename extensions to non-standard, but commonly found MIME
types.  This is initially a copy of the global <tt class="docutils literal"><span class="pre">common_types</span></tt> defined in the
module.</dd></dl>

<dl class="method">
<dt id="mimetypes.MimeTypes.guess_extension">
<tt class="descclassname">MimeTypes.</tt><tt class="descname">guess_extension</tt><big>(</big><em>type</em><span class="optional">[</span>, <em>strict</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mimetypes.MimeTypes.guess_extension" title="Permalink to this definition">¶</a></dt>
<dd>Similar to the <a title="mimetypes.guess_extension" class="reference internal" href="#mimetypes.guess_extension"><tt class="xref docutils literal"><span class="pre">guess_extension()</span></tt></a> function, using the tables stored as part
of the object.</dd></dl>

<dl class="method">
<dt id="mimetypes.MimeTypes.guess_type">
<tt class="descclassname">MimeTypes.</tt><tt class="descname">guess_type</tt><big>(</big><em>url</em><span class="optional">[</span>, <em>strict</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#mimetypes.MimeTypes.guess_type" title="Permalink to this definition">¶</a></dt>
<dd>Similar to the <a title="mimetypes.guess_type" class="reference internal" href="#mimetypes.guess_type"><tt class="xref docutils literal"><span class="pre">guess_type()</span></tt></a> function, using the tables stored as part of
the object.</dd></dl>

<dl class="method">
<dt id="mimetypes.MimeTypes.read">
<tt class="descclassname">MimeTypes.</tt><tt class="descname">read</tt><big>(</big><em>path</em><big>)</big><a class="headerlink" href="#mimetypes.MimeTypes.read" title="Permalink to this definition">¶</a></dt>
<dd>Load MIME information from a file named <em>path</em>.  This uses <a title="mimetypes.MimeTypes.readfp" class="reference internal" href="#mimetypes.MimeTypes.readfp"><tt class="xref docutils literal"><span class="pre">readfp()</span></tt></a> to
parse the file.</dd></dl>

<dl class="method">
<dt id="mimetypes.MimeTypes.readfp">
<tt class="descclassname">MimeTypes.</tt><tt class="descname">readfp</tt><big>(</big><em>file</em><big>)</big><a class="headerlink" href="#mimetypes.MimeTypes.readfp" title="Permalink to this definition">¶</a></dt>
<dd>Load MIME type information from an open file.  The file must have the format of
the standard <tt class="docutils literal"><span class="pre">mime.types</span></tt> files.</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../contents.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">19.7. <tt class="docutils literal"><span class="pre">mimetypes</span></tt> &#8212; Map filenames to MIME types</a><ul>
<li><a class="reference external" href="#mimetypes-objects">19.7.1. MimeTypes Objects</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="mimetools.html"
                                  title="previous chapter">19.6. <tt class="docutils literal docutils literal docutils literal"><span class="pre">mimetools</span></tt> &#8212; Tools for parsing MIME messages</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="mimewriter.html"
                                  title="next chapter">19.8. <tt class="docutils literal"><span class="pre">MimeWriter</span></tt> &#8212; Generic MIME file writer</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/mimetypes.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="mimewriter.html" title="19.8. MimeWriter — Generic MIME file writer"
             >next</a> |</li>
        <li class="right" >
          <a href="mimetools.html" title="19.6. mimetools — Tools for parsing MIME messages"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="netdata.html" >19. Internet Data Handling</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
      Last updated on Apr 15, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>